<form id="addOrUpdateForm" lay-filter="addOrUpdateForm" class="layui-form model-form">

    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">标题</label>
        <div class="layui-input-block">
            <input name="title" class="layui-input" placeholder="请输入标题" lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">作者</label>
        <div class="layui-input-block">
            <input name="author" class="layui-input" placeholder="请输入作者" lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label layui-form-required">发布状态</label>
            <div class="layui-input-block">
                <div id="releaseType" style="width: 184px"></div>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label layui-form-required">通知类型:</label>
            <div class="layui-input-block">
                <div id="noticeType" style="width: 184px"></div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label layui-form-required">发布时间</label>
            <div class="layui-input-block">
                <input type="text" name="publishTime" id="publishTime" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label layui-form-required">描述</label>
        <div class="layui-input-block">
            <textarea name="description" class="layui-textarea" placeholder="请输入描述" ></textarea>
        </div>
    </div>

    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="formSubmitBtn" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'form', 'admin', 'xnUtil', 'xmSelect', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var xnUtil = layui.xnUtil;
        var xmSelect = layui.xmSelect;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#publishTime'
        });

        var editData = admin.getLayerData('#addOrUpdateForm').data;
        if(editData !== null && editData !== undefined) {
            form.val('addOrUpdateForm', editData);
        }

        /**
         * 渲染字典
         * */
        var releaseType = xmSelect.render({
            el: '#releaseType',
            name: 'status',
            data: xnUtil.getDictDataByDictTypeCode('release_status', null),
            layVerify: 'required',
            initValue: editData ? [editData.status] : [],
            layVerType: 'tips',
            radio: true,
            clickClose: true,
            model: { icon:'hidden', label: { type: 'text' }},
            prop: {
                name: 'name',
                value: 'code'
            },
            tips: '请选择发布类型'
        });

        /**
         * 渲染字典
         * */
        var cmsNoticeType = xmSelect.render({
            el: '#noticeType',
            name: 'noticeType',
            data: xnUtil.getDictDataByDictTypeCode('cms_notice_type', null),
            layVerify: 'required',
            initValue: editData ? [editData.noticeType] : [],
            layVerType: 'tips',
            radio: true,
            clickClose: true,
            model: { icon:'hidden', label: { type: 'text' }},
            prop: {
                name: 'name',
                value: 'code'
            },
            tips: '请选择通知类型'
        });

        /**
         * 表单提交事件
         */
        form.on('submit(formSubmitBtn)', function (data) {
            layer.load(2);

            var url = editData ? getProjectUrl() + 'notice/edit' : getProjectUrl() + 'notice/add';
            admin.req(url, JSON.stringify(data.field), function(res){
                layer.msg(res.message, {icon: 1, time: 1000}, function () {
                    admin.putLayerData('formOk', true, '#addOrUpdateForm');
                    admin.closeDialog('#addOrUpdateForm');
                });
            }, 'post');
            return false;
        });
    });
</script>
</body>
</html>
